var fs = require("fs")
var commonUtil = require('./common-util')

/**
 * 导入制表符分隔的txt文件数据，可用Excel生成
 * 详细格式见example目录下tab-txt.txt文件
 * @param {文件地址} path 
 */
function importTabTxt(path) {
    var csv = fs.readFileSync(path).toString()
    var arr = csv.split('\r\n')
    var json = {}
    var arr1 = arr[1].split('\t')
    json.tableName = arr1[0]
    json.tableDesc = arr1[1]
    json.tableFields = []
    for (var i = 3; i < arr.length - 1; i++) {
        var tmpArr = arr[i].split('\t')
        var e = {};
        e.field = tmpArr[0]
        e.desc = tmpArr[3]
        e.type = tmpArr[1]
        e.required = tmpArr[2] == 'N' ? true : false
        if (!commonUtil.isNull(tmpArr[4])) {
            if (tmpArr[4] == 'pk') {
                e.tableId = true
            }
            if (tmpArr[4] == 'index') {
                e.index = true
            }
        }
        json.tableFields.push(e)
    }
    return json
}

module.exports = {
    importTabTxt: importTabTxt
}